package de.guice.aktenschrank.logutil;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.guice.aktenschrank.configuration.Annotation.Trace;

public class LogInterceptor implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation arg0) throws Throwable {

		Object ret = null;
		String exception = null;

		try {
			ret = arg0.proceed();
		} catch (Throwable t) {
			exception = t.getMessage();
		}
		Trace v = arg0.getMethod().getAnnotation(Trace.class);

		Logger LOGGER = LoggerFactory.getLogger(arg0.getMethod()
				.getDeclaringClass()
				+ "." + arg0.getMethod().getName());

		if (exception != null)
			LOGGER.trace(v.value() + " - ERROR - " + exception);
		else if (ret == null)
			LOGGER.trace(v.value());
		else
			LOGGER.trace(v.value() + " - " + ret.toString());

		return ret;
	}

}
